<?php

namespace app\admin\controller;

use think\Db;

class Topup extends Common
{
    public function unpayReceipt()
    {
        $where = [];
        $state=input('state/d', 0);
        $search_name=input('param.name');
        $search_type=input('param.search');

        if($search_name && $search_type !== null){
            $where[$search_type] = $search_name;
        }
        $where['state'] = $state;
        $data=DB::name('topup_unpay_receipt')
                ->where($where)
                ->field('id,state,numbers,type,pay_time,create_time,fk_userid,money,price,addtime')
                ->order('id desc')
                ->paginate(_getCachePages(),false,['query'=>request()->param()])
                ->each(function($item, $key){
                    return $item;
                });
        $this->assign('state',$state);
        $this->assign('data',$data);
        $page = $data->render();
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function setState($id, $state)
    {
        $res = Db::name('topup_unpay_receipt')->where(['id'=>$id])->update(['state'=>$state, 'deal_time'=>time()]);
        return $res?['state'=>1,'msg'=>'操作成功']:['state'=>0,'msg'=>'操作失败'];
    }

    public function limitRecords()
    {
        $where = [];

        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            if(in_array($search_type, ['id', 'phone'])){
                $where['t2.'.$search_type] = $search_name;
            }elseif(in_array($search_type, ['name'])){
                $where['t2.'.$search_type] = ['like', '%'.$search_name.'%'];
            }else{
                $where['t1.'.$search_type] = ['like', '%'.$search_name.'%'];
            }
        }

        $time = input('time');
        $type = input('type');

        if (!$time) {
            $time = date('Y-m-d');
        }
        $where['t1.time'] = ['between', [strtotime($time), strtotime($time . ' 23:59:59')]];

        $this->assign('time', $time);
        $this->assign('type', $type);

        if ($type) {
            $user_ids = Db::name('topup_temporary_quota')->where(['quota'=>['>',0], 'time'=>$where['t1.time']])->column('user_id');
            if ($type == 1) {
                $where['t1.user_id'] = ['in', $user_ids];
            } else {
                $where['t1.user_id'] = ['not in', $user_ids];
            }
        }

        $data = Db::name('topup_user_limit_records')->alias('t1')->join('user t2','t1.user_id=t2.id')->where($where)->field('t1.*,t2.name')->order('t1.user_id desc, t1.id desc')->paginate(_getCachePages(), false, ['query' => request()->param()])->each(function ($item) use ($where)
        {
            $item['quota'] = Db::name('topup_temporary_quota')->where(['user_id'=>$item['user_id'], 'time'=>$where['t1.time']])->value('quota');
            return $item;
        });

        $count = Db::name('topup_user_limit_records')->alias('t1')->join('user t2','t1.user_id=t2.id')->where($where)->field('t1.*,t2.name')->order('t1.user_id desc, t1.id desc')->group('user_id')->count();
        
        $this->assign('count', $count);
        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);
        return $this->fetch();
    }




    public function tempQuota()
    {
        if (request()->isAjax()) {
            $post = input('post.');

            $user = Db::name('user')->find($post['user_id']);

            $res = false;
            $time = time();
            $result = false;
            if ($user) {
                $topuplimit = Db::name('topup_temporary_quota')->where(['user_id'=>$post['user_id']])->whereTime('time', 'today')->find();
                if($topuplimit){
                    $topuplimitid = $topuplimit['id'];
                }else{
                    $topuplimitid = Db::name('topup_temporary_quota')->insertGetid(['user_id'=>$post['user_id'], 'time' => $time]);
                }
                if($post['value'] >= 0){
                    if ($post['value'] > 5000) {
                        return ['state' => 0, 'msg' => '当前可填写金额最大为 5000'];
                    }
                    $res = Db::name('topup_temporary_quota')->where(['id'=>$topuplimitid])->update(['quota'=>$post['value'], 'time' => $time]);
                }else{
                    $result = ['state' => 0, 'msg' => '错误值'];
                }
            }

            if ($res !== false) {
                $result = ['state' => 1, 'msg' => '操作成功'];
            } elseif(!$result) {
                $result = ['state' => 0, 'msg' => '操作失败'];
            }
            return $result;
        }else{
            $where = [];

            $search_name = input('param.name');
            $search_type = input('param.search');
            if ($search_name && $search_type) {
                if(in_array($search_type, ['id', 'phone'])){
                    $where['t2.'.$search_type] = $search_name;
                }elseif(in_array($search_type, ['name'])){
                    $where['t2.'.$search_type] = ['like', '%'.$search_name.'%'];
                }else{
                    $where['t1.'.$search_type] = ['like', '%'.$search_name.'%'];
                }
            }

            $start = input('start');
            $end = input('end');

            if ($start && $end) {
                $where['t1.time'] = ['between', [strtotime($start), strtotime($end)]];
            } else if ($start && !$end) {
                $where['t1.time'] = ['egt', strtotime($start)];
            } else if (!$start && $end) {
                $where['t1.time'] = ['elt', strtotime($end)];
            }
            $this->assign('start', $start);
            $this->assign('end', $end);

            $data = Db::name('topup_temporary_quota')->alias('t1')->join('user t2','t1.user_id=t2.id')->where($where)->field('t1.*,t2.name')->order('t1.id desc')->paginate(_getCachePages(), false, ['query' => request()->param()]);
            
            $this->assign('data', $data);
            $page = $data->render();
            $this->assign('page', $page);
            return $this->fetch();
        }
    }
    
    public function untempQuota()
    {
        $id = input('id');
        
        if (request()->isAjax()) {
            $value = input('value',0);
            $res = Db::name('topup_temporary_quota')->where('id', $id)->find();
            $data = ['err' => '设置失败'];
            if(is_numeric($value)){
                if ($res && $value >= 0) {
                    if ($value > 5000) {
                        return ['err' => '当前可填写金额最大为 5000'];
                    }
                    Db::name('topup_temporary_quota')->where('id', $id)->update(['quota'=>$value]);
                    $data = ['ret' => '设置成功'];
                }
            }
        } else {
            if ($id) {
                $name = Db::name('topup_temporary_quota')->where('id', $id)->find();
                if ($name) {
                    $this->assign('name', $name);
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }
        return $data;
    }
}